package logic

import (
	"context"

	"order/internal/model"
	"strconv"

	"gitee.com/xuxinzhi2206/corehub/gopay"
	"github.com/zeromicro/go-zero/core/logx"
	"order/internal/svc"
	"order/order"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderAddLogic) OrderAdd(in *order.OrderAddRequest) (*order.OrderAddResponse, error) {
	orderModel := model.NewOrderModel(l.ctx, l.svcCtx)
	createOrder, err := orderModel.CreateOrder(int(in.UserID), int(in.GoodsID))
	if err != nil {
		return nil, err
	}

	datago := gopay.PayData{
		OutTradeNo:  createOrder.OrderNo,
		TotalAmount: strconv.Itoa(createOrder.OrderNum),
		Subject:     "f",
		ReturnUrl:   "f",
		NotifyUrl:   "f",
	}

	data := gopay.NewPayData()

	_, err = data.Pay(&datago)
	if err != nil {
		return nil, err
	}

	return &order.OrderAddResponse{Add: &order.OrderInfo{
		OrderNo:    createOrder.CreatedAt,
		UserId:     int64(createOrder.UserId),
		GoodsId:    int64(createOrder.GoodsId),
		OrderNum:   int64(createOrder.OrderNum),
		OrderPrice: float32(createOrder.OrderPrice),
		CreatedAt:  createOrder.OrderNo,
	}}, nil
}
